package com.LostAndFound.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.LostAndFound.domain.Root;
import com.LostAndFound.utils.DataSourceUtils;

public class RootListDao {

	public void deleteSystemInfoByRid(String rid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "delete from info where rid = ?";
		runner.update(sql,rid);
	}
	// 检查用户名是否已经被占用
	public int checkAddUsername(String addUsername) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select count(*) from root where username = ?";
		Long row = (Long) runner.query(sql, new ScalarHandler(),addUsername);
		return row.intValue();
	}

	public int getTotalRootForEasyUI() throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select count(*) from root";
		Long total = (Long) runner.query(sql, new ScalarHandler());
		return total.intValue();
	}
	// 只展示普通管理员的数据
	public List<Root> getRootListForEasyUI(int startIndex, int pageSize) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from root where limitflag = 1 limit ?,?";
		return runner.query(sql, new BeanListHandler<Root>(Root.class),startIndex,pageSize);
	}

	public void handleDeleteRootForEasyUI(String rid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "delete from root where rid=?";
		runner.update(sql,rid);		
	}

	public void handleAddMangerEasyUI(Root root) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into root values(?,?,?,?,?,?,?,?,?)";
		Object[] params = {root.getRid(),root.getUsername(),root.getPassword(),
				root.getGender(),root.getCellphone(),root.getEmail(),
				root.getRealname(),root.getlimitFlag(),root.getResume()};
		runner.update(sql, params);
	}
	public void handleEditManagerEasyUI(Root root) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "update root set password=?,gender=?,cellphone=?,email=?,"
				+ "realname=?,limitFlag=?,resume=? where rid=?";
		Object[] params = {root.getPassword(),root.getGender(),root.getCellphone(),
				root.getEmail(),root.getRealname(),root.getlimitFlag(),
				root.getResume(),root.getRid()};
		runner.update(sql, params);
	}
}
